c   MEUnAgg.for generates uncertainty estimates

c    at 95, 50, 5th fractiles for ANWR aggregate

c    distributions. It also creates a file that gives the sample

c    id numbers of the play runs making up the uncertainty estimates.

c

c    Written by Schuenemeyer 4/3/98

c

c    Input files:

c     Unit  8 - Aggregate distribution file name, in-place or

c               or recoverable.

c    Output files:

c     Unit 10 - Uncertainty estimates

c     Unit 11 - Sample id numbers of uncertainty estimates

c

c    Subroutines required: buble.for (included)

c

      character title*80,filein*25,rei(2)*11,filaue*25,filidu*25

      dimension x(10000,8),id(10000),nar(63,8),nfr(3)

	dimension perc(3),xs(10000)	,fr(3,8)

	data perc/0.05,.5,.95/,rei/'In-Place   ','Recoverable'/

	data nfr/95,50,5/

	call getdat(iyrx,imonx,idayx)

	write(*,3)

    3 format(/' Program MEUnAgg.for - Uncertainty estimates for'

     1 ,' aggregate distributions'/)

    4 write(*,*)' In-place or recoverable, enter 1 or 2'

	read (*,*) ipr

	if(ipr.lt.1.or. ipr.gt.2) go to 4

	write(*,5)	rei(ipr)

    5 format(' Enter name of ',a9,' Distn file')

	read(*,'(a25)')filein

	open(8,file=filein,status='old')

	write(*,*)' Enter file name for Aggregate Uncertainty Estimates'

	read(*,'(a25)') filaue

c   This file has been called FraIPRes.dat and FraRes.dat for in-place

c    and recoverable uncertainty estimates respectively.

      open(10,file=filaue)

 	write(*,9)

    9 format(' Enter file name for Aggregate Uncertainty Sample'

     1 ,' Numbers')

	read(*,'(a25)') filidu

c   This file has been called FraIPID.dat and FraID.dat for in-place

c    and recoverable uncertainty estimates respectively.

	open(11,file=filidu)

c   num=10000 is the number of simulation run

	num=10000

 	nsper=10

	do i=1,8

	 do j=1,3

	 fr(j,i)=0.0

	 end do

	end do
	read(8,'(a80)')title

      do m=1,num

   	 read(8,*)it,(x(m,j),j=1,8)

	end do

c   do for each case	

	do ic =1,8

 	do m=1,num

	 xs(m)=x(m,ic)

	 id(m)=m

	end do

 	call buble(xs,id,num)

c   do for each percentile (fractile) loop

      do ki=1,3

      npid=int(perc(ki)*num+.001)

c   get size distribution

      nll=npid-nsper

	nul=npid+nsper

	ndif=nul-nll+1

	xndif=ndif

c   store id's  & get averages

      do i=nll,nul

	 ia=(i-nll+1)+(ki-1)*ndif

	 nar(ia,ic)=id(i)

	 fr(ki,ic)=fr(ki,ic)+xs(i)

	end do

	end do

	do ki=1,3

	 fr(ki,ic)=fr(ki,ic)/xndif

	end do

	end do

	write(10,44)

   44 format(1x,'Fractiles  TotOil   TotNAGas   1002Oil'

     1 '1002NAGas  Und1002Oil  Und1002NAGas  Def1002Oil Def1002NAGas')

	do ki=1,3

       write(10,46)nfr(ki),(fr(ki,ic),ic=1,8)

   46  format(i3,8f12.3)

	end do

	write(11,48)

   48 format(' ID Numbers of Observations-Link to Prospect-Rand.dat')

	do j=1,63

	 jj=(j-1)/21 + 1

	 write(11,50) j,nfr(jj),(nar(j,ic),ic=1,8)

   50  format(2i3,8i7)

	end do

	stop

      END

  

	SUBROUTINE BUBLE(X,ID,N)

      DIMENSION X(1),ID(1)

      KS=N

   15 KW=0

      DO 30 I=2,KS

      IF(X(I).GE.X(I-1)) GOTO 30

      TMP=X(I)

      X(I)=X(I-1)

      X(I-1)=TMP

      NTI=ID(I)

      ID(I)=ID(I-1)

      ID(I-1)=NTI

      KW=1

   30 CONTINUE

      IF(KW.EQ.0) RETURN

      KS=KS-1

      IF(KS.EQ.1) RETURN

      GOTO 15

      END

